package com.robot.merchant.controller;

import com.robot.merchant.constant.Constant;
import com.robot.merchant.pojo.Company;
import com.robot.merchant.pojo.Store;
import com.robot.merchant.service.CompanyService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import util.CheckUtil;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import static entity.Constant.MAX_SMS;

/**
 * company控制器层
 *
 * @author
 */
@RestController
@CrossOrigin
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;
    @Autowired
    private CheckUtil checkUtil;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(StatusCode.SUCCESS_QUERY, companyService.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        return new Result(StatusCode.SUCCESS_QUERY, companyService.findById(id));
    }


    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Company> pageList = companyService.findSearch(searchMap, page, size);
        return new Result(StatusCode.SUCCESS_QUERY, new PageResult<Company>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap) {
        return new Result(StatusCode.SUCCESS_QUERY, companyService.findSearch(searchMap));
    }


    /**
     * 修改
     *
     * @param company
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@RequestBody Company company, @PathVariable String id) {
        company.setId(id);
        companyService.update(company);
        return new Result(StatusCode.SUCCESS_UPDATED);
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
        companyService.deleteById(id);
        return new Result(StatusCode.SUCCESS_DELETED);
    }

    @RequestMapping(value = "/sendSms/{phone}")
    public Result sendSms(@PathVariable String phone) {
        if (!checkUtil.checkPhone(phone)) {
            return new Result( StatusCode.ERROR_INVALID_PHONE);
        }
        String redisKey = Constant.MERCHANT_LOGIN_SMS_COUNT + phone;

        if (redisTemplate.opsForValue().get(redisKey) == null) {
            redisTemplate.opsForValue().set(redisKey, 1, 1, TimeUnit.DAYS);
        }

        int count = (int) redisTemplate.opsForValue().get(redisKey);
        if (count >= MAX_SMS) {
            return new Result(StatusCode.ERROR_SEND_TOO_MANY_SMS);
        } else {
            count++;
            redisTemplate.opsForValue().set(redisKey, count, 1, TimeUnit.DAYS);
        }
        companyService.sendSms(phone);
        return new Result(StatusCode.SUCCESS_SEND);
    }

    @RequestMapping(value = "/login/phone/{checkCode}")
    public Result loginByPhone(@PathVariable String checkCode, @RequestBody Store store) {
        if (checkUtil.checkPhone(store.getPhone())) {
            return new Result( StatusCode.ERROR_INVALID_PHONE);
        }
        return null;
    }
}
